﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using APA_DAO;
using TAM_SPORT;

namespace AMS
{
    public partial class FrmEditThuePhongInfor : Form
    {
        private DataTable RoomTable = null;
        private ThuePhongDao dao;
        private int roomId;
        private bool ableToEdit = false;
        private int soThuePhong;
        public FrmEditThuePhongInfor()
        {
            InitializeComponent();
            
        }
        public FrmEditThuePhongInfor(int id, int roomID)
        {
            InitializeComponent();
            this.soThuePhong = id;
            dao = new ThuePhongDao();
            RoomTable = dao.getRoomList4Edit(roomID);
            this.roomId = roomID;
            loadCbx();
            loadInvoiceInfor(id);
            this.BackColor = Color.LightCyan;
        }

        private void loadCbx()
        {
            if (RoomTable != null)
            {
                cbxRoom.DataSource = RoomTable;
                cbxRoom.DisplayMember = "SO_PHONG";
                cbxRoom.ValueMember = "ID";
                cbxRoom.SelectedValue = this.roomId;
            }
        }

        private void loadInvoiceInfor(int soThuePhong)
        {
            DataRow row = dao.GetThuePhonGInfor4Edit(soThuePhong);
            if (row != null)
            {
                int flg = 0;
                if (int.TryParse(row["NCOUNT"].ToString(), out flg))
                {
                    if (flg == 1)
                    {
                        this.ableToEdit = true;
                        txtChiSoDien.Enabled = true;
                        txtChiSoNuoc.Enabled = true;
                        txtStartDate.Enabled = true;
                        dtpStartDate.Enabled = true;
                        nbrMonth.Enabled = true;
                    }
                    else
                    {
                        ableToEdit = false;
                        txtChiSoDien.Enabled = false;
                        txtChiSoNuoc.Enabled = false;
                        txtStartDate.Enabled = false;
                        dtpStartDate.Enabled = false;
                        nbrMonth.Enabled = false;
                    }
                }

                txtTienDacCoc.Text = float.Parse(row["TIEN_COC"].ToString()).ToString("#,##0.##");
                txtRoomPrice.Text = float.Parse(row["DON_GIA_PHONG"].ToString()).ToString("#,##0.##");
                txtStartDate.Text = row["NGAY_VAO_O"] == System.DBNull.Value ? "" : DateTime.Parse(row["NGAY_VAO_O"].ToString()).ToString("dd/MM/yyyy");
                txtChiSoDien.Text = float.Parse(row["CHI_SO_DIEN"].ToString()).ToString("#,##0.##");
                txtChiSoNuoc.Text = float.Parse(row["CHI_SO_NUOC"].ToString()).ToString("#,##0.##");
                nbrMonth.Value = int.Parse(row["SO_THANG"].ToString());
            }
        }

        private void cbxRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxRoom.SelectedIndex >= 0)
            {
                float roomPrice = 0;
                int roomID = 0;
                if(int.TryParse(cbxRoom.SelectedValue.ToString(), out roomID))
                {
                    string condition = "ID = " + roomID;
                    DataRow [] rows = RoomTable.Select(condition);
                    if(rows != null && rows.Length >0)
                    {
                        DataRow row = rows[0];
                        if (float.TryParse(row["DON_GIA"].ToString(), out roomPrice))
                        {
                            txtRoomPrice.Text = roomPrice.ToString("#,###.##");
                        }
                    }
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int flg = 2;
            if (this.ableToEdit)
            {
                flg = 1;
            }
            DateTime day;
            DateTime.TryParseExact(txtStartDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out day);
            
            if (validate())
            {
                if(dao.updateThuePhong(this.soThuePhong, int.Parse(cbxRoom.SelectedValue.ToString()), 
                    float.Parse(txtTienDacCoc.Text.Replace(",","")),
                    float.Parse(txtRoomPrice.Text.Replace(",","")),
                    float.Parse(txtChiSoNuoc.Text.Replace(",","")),
                    float.Parse(txtChiSoDien.Text.Replace(",","")),
                    day,(int)nbrMonth.Value, flg))
                {
                    Result re = new Result("Lưu thành công.");
                    re.ShowDialog();
                    re.Dispose();
                    this.Close();
                }else
                {
                    Error er = new Error("Lưu thất bại");
                    er.ShowDialog();
                    er.Dispose();
                }
            }
        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            txtStartDate.Text = dtpStartDate.Value.ToString("dd/MM/yyyy");
        }

        private bool validate()
        {
            bool errFlg = false;
            StringBuilder dtrBuilder = new StringBuilder();
            int i = 0;
            if (cbxRoom.SelectedIndex < 0)
            {
                i++;
                dtrBuilder.Append(i + ". Vui lòng chọn phòng.");
                errFlg = true;
            }
            float roomPrice =0;
            if(!float.TryParse(txtRoomPrice.Text.Replace(",", ""), out roomPrice))
            {
                i++;
                if (errFlg)
                {
                    dtrBuilder.Append("\n");
                }
                dtrBuilder.Append(i + ". Đơn giá phòng chưa được nhập hoặc nhập sai định dạng.");
                errFlg = true;
            }

            if (!float.TryParse(txtTienDacCoc.Text.Replace(",", ""), out roomPrice))
            {
                i++;
                if (errFlg)
                {
                    dtrBuilder.Append("\n");
                }
                dtrBuilder.Append(i + ". Tiền đặc cọc phải là kiểu số.");
                errFlg = true;
            }

            if (!float.TryParse(txtChiSoDien.Text.Replace(",", ""), out roomPrice))
            {
                i++;
                if (errFlg)
                {
                    dtrBuilder.Append("\n");
                }
                dtrBuilder.Append(i + ". Chỉ số điện phải là kiểu số.");
                errFlg = true;
            }

            if (!float.TryParse(txtChiSoNuoc.Text.Replace(",", ""), out roomPrice))
            {
                i++;
                if (errFlg)
                {
                    dtrBuilder.Append("\n");
                }
                dtrBuilder.Append(i + ". Chỉ số nước phải là kiểu số.");
                errFlg = true;
            }

            DateTime day;
            if (!DateTime.TryParseExact(txtStartDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out day))
            {
                i++;
                if (errFlg)
                {
                    dtrBuilder.Append("\n");
                }
                dtrBuilder.Append(i + ". Ngày bắt đầu vào ở sai định dạng.");
                errFlg = true;
            }

            if (!string.IsNullOrEmpty(dtrBuilder.ToString()))
            {
                Error er = new Error(dtrBuilder.ToString());
                er.ShowDialog();
                er.Dispose();
            }
            return !errFlg;
        }

        private void txtRoomPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private String RemoveComma(String value)
        {
            if (value != null && value.Contains(","))
            {
                value = value.Replace(",", String.Empty).Trim();
            }
            return value;
        }

        private void txtRoomPrice_TextChanged(object sender, EventArgs e)
        {
            if (RemoveComma(txtRoomPrice.Text).Length > 3)
            {
                txtRoomPrice.Text = decimal.Parse(RemoveComma(txtRoomPrice.Text)).ToString("#,###");
                txtRoomPrice.Select(txtRoomPrice.Text.Length, 0);
            }
        }

        private void txtStartDate_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtStartDate_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtTienDacCoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtTienDacCoc_TextChanged(object sender, EventArgs e)
        {
            if (RemoveComma(txtTienDacCoc.Text).Length > 3)
            {
                txtTienDacCoc.Text = decimal.Parse(RemoveComma(txtTienDacCoc.Text)).ToString("#,###");
                txtTienDacCoc.Select(txtTienDacCoc.Text.Length, 0);
            }
        }

        private void txtChiSoNuoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtChiSoDien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtChiSoNuoc_TextChanged(object sender, EventArgs e)
        {
            if (RemoveComma(txtChiSoNuoc.Text).Length > 3)
            {
                txtChiSoNuoc.Text = decimal.Parse(RemoveComma(txtChiSoNuoc.Text)).ToString("#,###");
                txtChiSoNuoc.Select(txtChiSoNuoc.Text.Length, 0);
            }
        }

        private void txtChiSoDien_TextChanged(object sender, EventArgs e)
        {
            if (RemoveComma(txtChiSoDien.Text).Length > 3)
            {
                txtChiSoDien.Text = decimal.Parse(RemoveComma(txtChiSoDien.Text)).ToString("#,###");
                txtChiSoDien.Select(txtChiSoDien.Text.Length, 0);
            }
        }
    }
}
